package com.anjiplus.template.gaea.generator.config;

import java.util.Properties;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;

import lombok.Data;

/**
 * https://blog.csdn.net/gabriel_2/article/details/46329461
 *
 *@author WongBin
 *@date 2019/8/13
 */
//@Configuration
@ConfigurationProperties(prefix = "spring.datasource")
/*@PropertySource("classpath:data_source.properties")
@EnableJpaRepositories(
		basePackages = {"com.anjiplus.template.gaea.generator.repository"},
		entityManagerFactoryRef = "baseEntityManagerFactory",
		transactionManagerRef = "baseTransactionManager")*/
@Data
//@ConditionalOnExpression("${generator.enabled:false}")
public class DbConfig {
	private String url;
	private String username;
	private String password;
	private String driverClassName;
	private int initialSize;
	private int minIdle;
	private int maxActive;
	private int maxWait;
	private int timeBetweenEvictionRunsMillis;
	private int minEvictableIdleTimeMillis;
	private String validationQuery;
	private boolean testWhileIdle;
	private boolean testOnBorrow;
	private boolean testOnReturn;
	private boolean poolPreparedStatements = true;
	private int maxPoolPreparedStatementPerConnectionSize = 10;
	private int connectionErrorRetryAttempts = 3;
	private boolean breakAfterAcquireFailure = false;

	@Bean(name = "baseDataSource", initMethod = "init", destroyMethod = "close")
	@Qualifier("baseDataSource")
	public DruidDataSource mysqlDataSource() {
		DruidDataSource datasource = new DruidDataSource();
		datasource.setUrl(url);
		datasource.setUsername(username);
		datasource.setPassword(password);
		datasource.setDriverClassName(driverClassName);
		/*datasource.setInitialSize(initialSize);
		datasource.setMinIdle(minIdle);
		datasource.setMaxActive(maxActive);
		datasource.setMaxWait(maxWait);
		datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
		datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
		datasource.setValidationQuery(validationQuery);
		datasource.setTestWhileIdle(testWhileIdle);
		datasource.setTestOnBorrow(testOnBorrow);
		datasource.setTestOnReturn(testOnReturn);*/
		/*datasource.setPoolPreparedStatements(poolPreparedStatements);
		datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
		datasource.setConnectionErrorRetryAttempts(connectionErrorRetryAttempts);
		datasource.setBreakAfterAcquireFailure(breakAfterAcquireFailure);
		datasource.setKeepAlive(true);*/
		/*datasource.setRemoveAbandoned(false);
		datasource.setRemoveAbandonedTimeout(360000);
		datasource.setLogAbandoned(true);*/
		return datasource;
	}

	/*@Bean(name = "baseEntityManagerFactory")
	public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
		LocalContainerEntityManagerFactoryBean factory =
				new LocalContainerEntityManagerFactoryBean();
		factory.setDataSource(mysqlDataSource());
		*//*factory.setPersistenceProviderClass(HibernatePersistenceProvider.class);
		factory.setPackagesToScan(env.getRequiredProperty(PACKAGES_TO_SCAN).split(","));*//*
		factory.setJpaProperties(hibernateProperties());
		factory.afterPropertiesSet();
		return factory;
	}
	@Resource
	protected Environment env;

	protected Properties hibernateProperties() {
		Properties properties = new Properties();
		properties.put("hibernate.ddl-auto",env.getProperty("spring.jpa.hibernate.ddl-auto"));
		properties.put("properties.hibernate.dialect",
				env.getProperty("spring.jpa.properties.hibernate.dialect"));
		properties.put("database-platform",
				env.getProperty("spring.jpa.database-platform"));
		properties.put("hibernate.naming_strategy",
				env.getProperty("spring.jpa.hibernate.naming_strategy"));
		properties.put("hibernate.physical_naming_strategy",
				env.getProperty("spring.jpa.hibernate.physical_naming_strategy"));
		properties.put("database",env.getProperty("spring.jpa.database"));
		return properties;
	}

	@Bean
	public EntityManager entityManager(){
		return entityManagerFactory().getObject().createEntityManager();
	}

	@Bean(name = "baseTransactionManager")
	public PlatformTransactionManager transactionManager() {
		JpaTransactionManager manager = new JpaTransactionManager();
		manager.setEntityManagerFactory(entityManagerFactory().getObject());
		manager.setDataSource(mysqlDataSource());
		return manager;
	}*/
	//https://blog.csdn.net/gabriel_2/article/details/46329461

	@Bean
	public JdbcTemplate baseJdbcTemplate(
			@Qualifier("baseDataSource") DataSource dataSource){
		return new JdbcTemplate(dataSource);
	}

}
